package com.example.myapplication6;

import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private Order order = new Order();
    private List<Fragment> fragments = new ArrayList<>();
    private MyOrdersFragment myOrdersFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.viewPager);

        // 初始化“我的订单”Fragment
        myOrdersFragment = new MyOrdersFragment();

        // 初始化4个步骤的Fragment
        Step1Fragment step1 = new Step1Fragment();
        step1.setOrder(order);
        fragments.add(step1);

        Step2Fragment step2 = new Step2Fragment();
        step2.setOrder(order);
        fragments.add(step2);

        Step3Fragment step3 = new Step3Fragment();
        step3.setOrder(order);
        fragments.add(step3);

        Step4Fragment step4 = new Step4Fragment();
        step4.setOrder(order);
        fragments.add(step4);

        // 设置ViewPager2适配器
        viewPager.setAdapter(new FragmentStateAdapter(this) {
            @Override
            public int getItemCount() {
                return fragments.size();
            }

            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }
        });
    }

    public void goToStep(int stepIndex) {
        viewPager.setCurrentItem(stepIndex);
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    // 提交订单后，跳转到“我的订单”并添加新订单
    public void submitOrderSuccess() {
        myOrdersFragment.addOrder(order);
        // 这里可跳转到“我的订单”页面（示例：用FragmentTransaction切换）
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.viewPager, myOrdersFragment)
                .commit();
        showToast("订单已提交，可在“我的订单”中查看");
    }
}